package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingProjectPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.service.NursingProjectPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description NursingPlanServiceImpl
 * @Author LiShunSen
 * @Date 2024-08-27
 */
@Service
public class NursingPlanServiceImpl implements NursingPlanService {

    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;
    @Autowired
    private NursingProjectPlanService nursingProjectpalnService;

    /**
     * 查询所有护理计划
     *
     * @return
     */
    public List<NursingPlanVo> getPlan() {
        return nursingPlanMapper.getPlan();
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlanDto
     * @return
     */
    @Transactional
    public void addPlan(NursingPlanDto nursingPlanDto) {

        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        //计划默认启用状态
        nursingPlan.setStatus(1);
        //在计划表中建立数据
        nursingPlanMapper.addPlan(nursingPlan);

        //在中间表中加数据
        List<NursingProjectPlanDto> projectPlans = nursingPlanDto.getProjectPlans();

            for (NursingProjectPlanDto nursingProjectPlanDto : projectPlans) {
                nursingProjectPlanDto.setPlanId(nursingPlan.getId());
            }
            nursingProjectpalnService.addList(projectPlans);

    }

    /**
     * 分页查询
     *
     * @param name
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageResponse<NursingPlanVo> getByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        //启动分页插件
        PageHelper.startPage(pageNum, pageSize);
        //查询
        Page<NursingPlanVo> page = nursingPlanMapper.getByPage(name, status);


        PageResponse<NursingPlanVo> pageResponse = new PageResponse<>(page);
        pageResponse.setRecords(page.getResult());

        return pageResponse;
    }


    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    public NursingPlanVo getId(Long id) {
        //查询护理计划 根据关联表的plan_id
        NursingPlanVo nursingPlan = nursingPlanMapper.getPlanById(id);
        //查询护理项目 根据关联表的project_id
        List<NursingProjectPlanVo> nursingProjectPlanVo = nursingProjectPlanMapper.getProjectById(id);

        nursingPlan.setProjectPlans(nursingProjectPlanVo);

        return nursingPlan;
    }

    @Transactional
    public void updatePlan(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        //修改护理计划的数据
        nursingPlanMapper.updatePlan(nursingPlan);

        List<NursingProjectPlanDto> projectPlans = nursingPlanDto.getProjectPlans();
        //删除护理项目
        List<Long> ids = getById(nursingPlanDto.getId());
        //新增护理项目
        for (NursingProjectPlanDto nursingProjectPlanDto : projectPlans) {
            nursingProjectPlanDto.setPlanId(nursingPlan.getId());
        }
        nursingProjectpalnService.deletList(ids);
        nursingProjectpalnService.addList(projectPlans);

    }

    //查询计划的ids
    private List<Long> getById(Long planId) {
        return nursingProjectPlanMapper.getById(planId);

    }

    @Override
    public void deletePlan(Long id) {
        nursingPlanMapper.deletePlan(id);
    }

    /**
     * 启用/禁用护理计划
     *
     * @param id
     * @param status
     * @return
     */
    public void statusPlan(Long id, Integer status) {

        nursingPlanMapper.statusPlan(id, status);
    }

}
